<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'title' => 'bool.py',
	'subtitle' => 'Written in <span title="Programming Fundamentals">CS 1101</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalized on 2017-03-08',
	'copyright year' => '2017',
	'body' => <<<END
<h2>Flowchart</h2>
<img class="weblog-header-image" src="/img/CC_BY-SA_4.0/y.st./coursework/CS1101/bool.py.png" alt="A flowchart depicting the flow of the script below" width="625" height="1284" />
<h2>Script:</h2>
<blockquote>
<pre><code># Every good program should begin with a license comment that declares the file
# as being covered by a free software license.
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see &lt;https://www.gnu.org./licenses/&gt;.

# This makes Python2 behave like Python3 in regards to user input.
if &quot;raw_input&quot; in vars(__builtins__):
	input = raw_input

# This function compares two numbers and returns what is basically the three-way
# equivalent of a boolean, but as an integer.
def compare(a, b):
	if(a &gt; b):
		return 1
	elif(a == b):
		return 0
# If a isn&apos;t greater than b and a isn&apos;t equal to b, a must be less than b.
#There&apos;s no reason to even check to see if a is less than b.
	else:
		return -1

# User input is in string form, not number form. We need to not only make sure
# the user input a number, but also convert the string representation of that
# number into a numeric representation.
def sanitize_and_parse(string):
	try:
# If the input can be treated as an integer, we&apos;ll do that.
		return int(string)
	except:
		try:
# If the input cannot be treated as an integer but can be treated as a float,
# we&apos;ll do that instead.
			return float(string)
		except:
# If the input isn&apos;t numeric, we&apos;ll simply treat it as zero.
			return 0

# This is a dummy function. We&apos;ll use it if we&apos;re in string mode.
def return_argument(arg):
	return arg

handle_input = None

# This lets us toggle between string mode and numeric mode.
while not handle_input:
	i = input(&apos;Enter &quot;0&quot; or &quot;s&quot; to compare strings.\\nEnter &quot;1&quot; or &quot;n&quot; to compare numbers.\\n&gt;&gt;&gt; &apos;)
	if i == &quot;0&quot; or i == &quot;s&quot;:
		handle_input = return_argument
		input_type = &quot;string&quot;
	elif i == &quot;1&quot; or i == &quot;n&quot;:
		handle_input = sanitize_and_parse
		input_type = &quot;number&quot;
	else:
		print(&quot;Invalid selection; please try again.&quot;)

# We need to run the function three times.
iterations = 3

# Zero evaluates as False. &quot;while iterations&quot; will work as expected, we don&apos;t
# need &quot;while iterations != 0&quot;.
while iterations:
	a = input(&quot;Please enter the first &quot;+input_type+&quot;.\\n&gt;&gt;&gt; &quot;)
	a = handle_input(a)
	b = input(&quot;Please enter the second &quot;+input_type+&quot;.\\n&gt;&gt;&gt; &quot;)
	b = handle_input(b)
# In Python2, the &quot;print&quot; key word won&apos;t output the correct message if we use
# multiple arguments to print() without removing the parentheses. However,
# removing the parentheses causes a syntax error in Python3. The only option
# that seems to work in both versions of the language is to concatenate the two
# things we need to print. We can&apos;t concatenate though without casting to string
# first, so we also have to do that.
	print(&quot;Return code: &quot;+str(compare(a, b)))
	iterations -= 1</code></pre>
</blockquote>
<h2>Output:</h2>
<blockquote>
<pre>Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information.
&gt;&gt;&gt; ================================ RESTART ================================
&gt;&gt;&gt; 
Enter &quot;0&quot; or &quot;s&quot; to compare strings.
Enter &quot;1&quot; or &quot;n&quot; to compare numbers.
&gt;&gt;&gt; n
Please enter the first number.
&gt;&gt;&gt; 5
Please enter the second number.
&gt;&gt;&gt; 2
Return code: 1
Please enter the first number.
&gt;&gt;&gt; 2
Please enter the second number.
&gt;&gt;&gt; 5
Return code: -1
Please enter the first number.
&gt;&gt;&gt; 3
Please enter the second number.
&gt;&gt;&gt; 3
Return code: 0
&gt;&gt;&gt;</pre>
</blockquote>
END
);
